// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import gameData from "../gameData";
import languageManager from "../languageManager/languageManager";
import randomData from "./randomData";

const {ccclass, property} = cc._decorator;

@ccclass
export default class randomShowSkin extends cc.Component {

    @property(cc.Node)
    item : cc.Node = null;

    @property(cc.Label)
    times : cc.Label = null;

    itemNum = 6;

    lastIndexList = [];

    maxListLength = 13;

    lastClickIndex = -1;

    labStringList = null;
    // onLoad () {}

    start () {

        this.labStringList = languageManager.getRandomShowSkinString();

        if(this.lastIndexList.length == 0){
            this.lastIndexList  = this.randomGetRoleSkin(this.itemNum,this.maxListLength,this.lastIndexList);
        }else{
            if(randomData.isUpdate()){
                this.lastIndexList  = this.randomGetRoleSkin(this.itemNum,this.maxListLength,this.lastIndexList);
            }
        }
        this.initRoleSkinContent(this.itemNum,this.lastIndexList);
        this.schedule(this.openUpdateSchedule,1);

        let lab = this.node.getChildByName("lab");
        lab.getComponent(cc.Label).string = this.labStringList.layer_0[0];
    }

    openUpdateSchedule(){

        if(randomData.isUpdate()){
            this.updateSkinList(this.itemNum);
        }

        let data = randomData.backCountTimes();
        if(data == null){
            this.times.string = "00:00";
        }else{
            if((randomData.getRoleUsingTimes() - 1 - data[0]) % 10 < 0){
                this.times.string = "00:00";
                gameData.randomSkinIndex = -1;
                cc.game.emit("resetRoleSkin");
            }else{
                if(Math.floor((60 - data[1]) / 10) > 0){
                    this.times.string = (randomData.getRoleUsingTimes() - 1 - data[0]) + ":" + (60 - data[1]);
                }else{
                    this.times.string = (randomData.getRoleUsingTimes() - 1 - data[0]) + ":0" + (60 - data[1]);
                }
            }
            
        } 
    }

    protected onDestroy(): void {
        this.unschedule(this.openUpdateSchedule);
    }

    /* 
    @param num 随机数量
    @param max 随机的最大值
    @param list 上次随机的列表 
    */
    randomGetRoleSkin(num,max,list){
        let curList = [];
        let index = 0;
        while(index < num){
            let random = Math.random() * max | 0;
            if(curList.indexOf(random) < 0 &&  list.indexOf(random) < 0){
                curList.push(random);
                index++;
            }
        }
        return curList;
    }

    /* 
    @param num 初始化人物的个数
    */
    initRoleSkinContent(num,list){
        let content = this.node.getChildByName("content");
        let gap = content.getComponent(cc.Layout).spacingX; 
        for(let i = 0 ; i < num ; i++){
            if(i != 0){
                let item = cc.instantiate(this.item);
                item.setPosition(cc.v2(-content.width / 2 + (gap + item.width) * i + item.width / 2,0))
                item.parent = content;
            }else{
                this.item.setPosition(cc.v2(-content.width / 2 + (gap + this.item.width) * i + this.item.width / 2,0))
            }
        }

        console.log("list",list);
        for(let i = 0 ; i < num ; i++){
            let content = this.node.getChildByName("content");
            this.addButtonEvent(content.children[i],i);

            if(list[i] < 9){
                cc.loader.loadRes("anim/zhujue",sp.SkeletonData,(err,res)=>{
                    if(err){
                        console.log(err);
                        return
                    }
                    let anim = content.children[i].getChildByName("anim");
                    let spk = anim.getComponent(sp.Skeleton);
                    spk.skeletonData = res;
                    spk.setSkin("pf0" +  (list[i] + 1));
                    spk.setAnimation(0,"animation",true);
                });
            }else{
                if(list[i] == 9){
                    cc.loader.loadRes("anim/zhujue",sp.SkeletonData,(err,res)=>{
                        if(err){
                            console.log(err);
                            return
                        }
                        let anim = content.children[i].getChildByName("anim");
                        let spk = anim.getComponent(sp.Skeleton);
                        spk.skeletonData = res;
                        spk.setSkin("pf" +  (list[i] + 1));
                        spk.setAnimation(0,"animation",true);
                    });
                }else if(list[i] == 10){
                    cc.loader.loadRes("anim/zhujue19",sp.SkeletonData,(err,res)=>{
                        if(err){
                            console.log(err);
                            return
                        }
                        let anim = content.children[i].getChildByName("anim");
                        let spk = anim.getComponent(sp.Skeleton);
                        spk.skeletonData = res;
                        spk.setSkin("pf1");
                        spk.setAnimation(0,"animation",true);
                    });
                }else if(list[i] == 11){
                    cc.loader.loadRes("anim/zhujue16",sp.SkeletonData,(err,res)=>{
                        if(err){
                            console.log(err);
                            return
                        }
                        let anim = content.children[i].getChildByName("anim");
                        let spk = anim.getComponent(sp.Skeleton);
                        spk.skeletonData = res;
                        spk.setSkin("pf1");
                        spk.setAnimation(0,"animation",true);
                    });
                }else if(list[i] == 12){
                    cc.loader.loadRes("anim/zhujue19",sp.SkeletonData,(err,res)=>{
                        if(err){
                            console.log(err);
                            return
                        }
                        let anim = content.children[i].getChildByName("anim");
                        let spk = anim.getComponent(sp.Skeleton);
                        spk.skeletonData = res;
                        spk.setSkin("pf01");
                        spk.setAnimation(0,"animation",true);
                    });
                }
            }
        }
    }

    addButtonEvent(node,index){
        let button = node.addComponent(cc.Button);
        let clickEventHandler = new cc.Component.EventHandler();
        clickEventHandler.target = this.node;
        clickEventHandler.component = "randomShowSkin";
        clickEventHandler.handler = "labBtnClickEvent";
        clickEventHandler.customEventData = `${index}`;
        button.clickEvents.push(clickEventHandler);
    }

    labBtnClickEvent(event,data){
        console.log(data,this.lastIndexList[data]);
        if(this.lastIndexList[data] == gameData.randomSkinIndex) return;
        randomData.setStartUseSkin(true);
        randomData.resetRoleUsingTimes();
        gameData.randomSkinIndex = this.lastIndexList[data];
        cc.game.emit("resetRoleSkin");
        this.setRoleLightStatus(parseInt(data));
    }

    setRoleLightStatus(index){
        let content = this.node.getChildByName("content");
        if(this.lastClickIndex != -1){
            let last_light = content.children[this.lastClickIndex].getChildByName("light");
            last_light.active = false;

            cc.tween(content.children[this.lastClickIndex])
                .to(0.1,{y : 0})
                .start();
        }
        let light = content.children[index].getChildByName("light");
        light.active = true;

        cc.tween(content.children[index])
            .to(0.1,{y : 50})
            .start();

        this.lastClickIndex = index;
    }

    updateSkinList(num){
        this.lastIndexList = this.randomGetRoleSkin(this.itemNum,this.maxListLength,this.lastIndexList);
        let list = this.lastIndexList;
        let content = this.node.getChildByName("content");
        for(let i = 0 ; i < num ; i++){
            if(list[i] < 9){
                cc.loader.loadRes("anim/zhujue",sp.SkeletonData,(err,res)=>{
                    if(err){
                        console.log(err);
                        return
                    }
                    let anim = content.children[i].getChildByName("anim");
                    let spk = anim.getComponent(sp.Skeleton);
                    spk.skeletonData = res;
                    spk.setSkin("pf0" +  (list[i] + 1));
                    spk.setAnimation(0,"animation",true);
                });
            }else{
                if(list[i] == 9){
                    cc.loader.loadRes("anim/zhujue",sp.SkeletonData,(err,res)=>{
                        if(err){
                            console.log(err);
                            return
                        }
                        let anim = content.children[i].getChildByName("anim");
                        let spk = anim.getComponent(sp.Skeleton);
                        spk.skeletonData = res;
                        spk.setSkin("pf" +  (list[i] + 1));
                        spk.setAnimation(0,"animation",true);
                    });
                }else if(list[i] == 10){
                    cc.loader.loadRes("anim/zhujue19",sp.SkeletonData,(err,res)=>{
                        if(err){
                            console.log(err);
                            return
                        }
                        let anim = content.children[i].getChildByName("anim");
                        let spk = anim.getComponent(sp.Skeleton);
                        spk.skeletonData = res;
                        spk.setSkin("pf1");
                        spk.setAnimation(0,"animation",true);
                    });
                }else if(list[i] == 11){
                    cc.loader.loadRes("anim/zhujue16",sp.SkeletonData,(err,res)=>{
                        if(err){
                            console.log(err);
                            return
                        }
                        let anim = content.children[i].getChildByName("anim");
                        let spk = anim.getComponent(sp.Skeleton);
                        spk.skeletonData = res;
                        spk.setSkin("pf1");
                        spk.setAnimation(0,"animation",true);
                    });
                }else if(list[i] == 12){
                    cc.loader.loadRes("anim/zhujue19",sp.SkeletonData,(err,res)=>{
                        if(err){
                            console.log(err);
                            return
                        }
                        let anim = content.children[i].getChildByName("anim");
                        let spk = anim.getComponent(sp.Skeleton);
                        spk.skeletonData = res;
                        spk.setSkin("pf01");
                        spk.setAnimation(0,"animation",true);
                    });
                }
            }
            let light = content.children[i].getChildByName("light");
            light.active = false;
            content.children[i].y = 0;
        }
        this.lastClickIndex = -1;
    }

    // update (dt) {}
}
